package com.banshuibao.bsb_android.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;

import com.banshuibao.bsb_android.MyApplication;

import java.io.File;
import java.lang.reflect.Method;

public class MediaUtil {

    /**
     * 截取视频第一帧
     *
     * @param context
     * @param uri
     * @return
     */
    public static Bitmap getVideoFirstFrame(Context context, Uri uri) {
        Bitmap bitmap = null;
        String className = "android.media.MediaMetadataRetriever";
        Object objectMediaMetadataRetriever = null;
        Method release = null;
        try {
            objectMediaMetadataRetriever = Class.forName(className).newInstance();
            Method setDataSourceMethod = Class.forName(className).getMethod("setDataSource", Context.class, Uri.class);
            setDataSourceMethod.invoke(objectMediaMetadataRetriever, context, uri);
            Method getFrameAtTimeMethod = Class.forName(className).getMethod("getFrameAtTime");
            bitmap = (Bitmap) getFrameAtTimeMethod.invoke(objectMediaMetadataRetriever);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (release != null) {
                    release.invoke(objectMediaMetadataRetriever);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }

    public static File createFile(String filePath, String title) {
        File file = null;
        File paths = MyApplication.applicationInstance.getApplicationContext().getExternalFilesDir(null);
        if (paths == null) {
            XToast.showToast(MyApplication.applicationInstance.getApplicationContext(), "创建文件失败");
            return null;
        }
        long memorySpace = paths.getFreeSpace() / 1024 / 1024;
        if (memorySpace <= 200) {
            XToast.showToast(MyApplication.applicationInstance.getApplicationContext(), "您的存储空间不足,无法下载该视频");
            return null;
        }
        File dir = new File(paths + "/" + filePath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        String path = dir + "/" + title + ".mp4";
        file = new File(path);
        return file;
    }

    public static File createAudioFile(String filePath, String title) {
        File file = null;
        File paths = MyApplication.applicationInstance.getApplicationContext().getExternalFilesDir(null);
        if (paths == null) {
            XToast.showToast(MyApplication.applicationInstance.getApplicationContext(), "创建文件失败");
            return null;
        }
        long memorySpace = paths.getFreeSpace() / 1024 / 1024;
        if (memorySpace <= 200) {
            XToast.showToast(MyApplication.applicationInstance.getApplicationContext(), "您的存储空间不足,无法下载该音频");
            return null;
        }
        File dir = new File(paths + "/" + filePath);
        if (!dir.exists()) {
            dir.mkdir();
        }
        String path = dir + "/" + title;
        file = new File(path);
        return file;
    }

    public static File getAudioFile(String dir, String uuid) {
        return new File(MyApplication.applicationInstance.
                getApplicationContext().getExternalFilesDir(null) +
                "/" + dir + MyApplication.applicationInstance.getLoginInfo().getUserId()
                + "/" +
                uuid);
    }

    public static File getMediaFile(String filePath, String videoId) {
        return new File(MyApplication.applicationInstance.
                getApplicationContext().getExternalFilesDir(null) +
                "/" + filePath + MyApplication.applicationInstance.getLoginInfo().getUserId()
                + "/" +
                videoId + ".mp4");
    }

}
